package com.example.administrator.activity.parcitise.Activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.activity.R;

import java.text.DecimalFormat;

public class BMI_2Activity extends AppCompatActivity {

    EditText bmi2_height2,bmi2_weight2;
    Button bmi2_cal,bmi2_about,bmi2_exit;
    TextView bmi2_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bmi_2);

        init();

        View.OnClickListener clickListener=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.bmi2_cal:
                        cal();
                        break;
                    case R.id.bmi2_about:
                        about();
                        break;
                    case R.id.bmi2_exit:
                        exit();
                        break;
                }
            }
        };
        bmi2_cal.setOnClickListener(clickListener);
        bmi2_about.setOnClickListener(clickListener);
        bmi2_exit.setOnClickListener(clickListener);
        bmi2_height2.setOnClickListener(clickListener);
        bmi2_weight2.setOnClickListener(clickListener);
        bmi2_result.setOnClickListener(clickListener);
    }

    private void exit() {

    }

    private void about() {

    }

    private void cal() {
        String s_height=bmi2_height2.getText().toString();
        double height=Double.parseDouble(s_height);
        String s_weight=bmi2_weight2.getText().toString();
        double weight=Double.parseDouble(s_weight);
        double bmi=weight/(height/100)/(height/100);

        DecimalFormat format=new DecimalFormat("0.00");
        String s="您的体重指数为："+format.format(bmi)+"\n 理想指数为20-22";
        if (bmi<18.5){
            s+="过轻";
        }else if (bmi<23){
            s+="正常";
        }else if (bmi<25){
            s+="过重";
        }else if (bmi<30){
            s+="肥胖";
        }else {
            s+="痴肥";
        }
        bmi2_result.setText(s);


    }

    private void init() {
        bmi2_cal=findViewById(R.id.bmi2_cal);
        bmi2_about=findViewById(R.id.bmi2_about);
        bmi2_exit=findViewById(R.id.bmi2_exit);
        bmi2_height2=findViewById(R.id.bmi2_height_2);
        bmi2_weight2=findViewById(R.id.bmi2_weight_2);
        bmi2_result=findViewById(R.id.bmi2_result);
    }
}
